package com.jing.ads.downloader;

import java.io.File;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.Log;

import com.jing.ads.R;
import com.jing.ads.application.GlobalConstants;
import com.jing.ads.util.GlobalUtil;

public class DownloadReceiver extends BroadcastReceiver {
	private static final String TAG = DownloadReceiver.class.getSimpleName();
	
	public static final int INSTALL_SUCCEEDED = 1;
	public static final int INSTALL_FAILED_APK_NOT_EXIST = -1;
	public static final int INSTALL_FAILED_APK_PARSER_ERROR = -2;
   
	@Override
	public void onReceive(Context context, Intent intent) {
		Uri uri;
		
		if(GlobalConstants.DEBUG) {
			Log.d(TAG, "onReceive===intent.getAction(): " + intent.getAction());
		}

		if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
			if (Constants.LOGVV) {
				Log.v(Constants.TAG, "Receiver onConnectivity");
			}
			NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
			if (info != null && info.isConnected()) {
				context.startService(new Intent(context, DownloadService.class));
//				GlobalUtil.startLogService(context, context.getPackageManager());
			}
		} else if (intent.getAction().equals(Constants.ACTION_RETRY)) {
			if (Constants.LOGVV) {
				Log.v(Constants.TAG, "Receiver retry");
			}
			context.startService(new Intent(context, DownloadService.class));
        } else if(intent.getAction().equals(GlobalConstants.ACTION_SYSINSTALL_APK)) {
        	uri = intent.getData();
	        if (uri == null) {
	            throw new IllegalArgumentException();
	        }
	        installPackageBySystem(context, uri);
        } else if(intent.getAction().equals(Downloads.DOWNLOAD_COMPLETED_ACTION)) {//下载完成
        	uri = intent.getData();
	        if (uri != null) {
	        	installPackageBySystem(context, uri);
	        }
        }
	}
	
	private void installPackageBySystem(Context context, Uri uri) {
		ContentResolver contentResolver = context.getContentResolver();
		Cursor cursor = contentResolver.query(uri, new String[] {
				Downloads.COLUMN_APP_ID, 
				Downloads.COLUMN_TITLE, 
				Downloads.COLUMN_NOTIFICATION_EXTRAS,
				Downloads._DATA }, null, null, null);
		
		if(cursor != null && cursor.moveToFirst()) {
			int appId = cursor.getInt(0);
			String appName = cursor.getString(1);
			String packageName = cursor.getString(2);
			String filePath = cursor.getString(3);
			
			cursor.close();
			
			Intent intentDownload;
			if(filePath != null) {
			    GlobalUtil.startInstall(context, new File(filePath));
	            
	            intentDownload = new Intent(GlobalConstants.DOWNLOAD_COMPLETE);
			} else {
//			    GlobalUtil.shortToast(context, R.string.downlod_error_msg);
			    
			    intentDownload = new Intent(GlobalConstants.DOWNLOAD_ERROR);
			}
			
			intentDownload.putExtra("appId", appId);
            intentDownload.putExtra("packageName", packageName);
            context.sendBroadcast(intentDownload);
		}
    }
	
}
